$(function(){
	$("#loading-first").fadeOut(500);
	
	
	laydate.render({
	  elem: '#startDate'
	});
	laydate.render({
	  elem: '#endDate'
	});
    {if $Think.get.insurance_id}
		var where_json=JSON.stringify({insurance_id:["in","{$Think.get.insurance_id}"]})
		Load()
		$.ajax({
			type:'post',
			data:{
				page:1,
				pagnum:10,
				where_json:where_json
			},
			url:"{:url('adminapi/Insurance/insuranceList')}",
			success:function(res){
				console.log(res)
				Load(1)
				if(res.code==1){
					var detail;
					for(var ii=0;ii<res.list.length;ii++){
						if(res.list[ii].insurance_id=={$Think.get.insurance_id}){
							detail=res.list[ii]
						}
					}
					
					for(var key in detail){
						$(`input[name=${key}]`).val(detail[key])
						$(`select[name=${key}]`).val(detail[key])
						$(`textarea[name=${key}]`).val(detail[key])
					}
				}else{
					Err(res.msg)
				}
			},
			error:function(){
				Load(1)
				Err('请求失败')
			}
		})
	
	{/if}
	
	
	var data={}
	{if $Think.get.insurance_id}
	$('.keep2').on('click',function(){
		data.company_id=$('.company_all_id').val();
		$('input').each(function(){
			if($(this).attr('name')){
				data[$(this).attr('name')]=$(this).val()
			}
		})
		data.scale=$('select[name="scale"]').val()
		data.remark=$('textarea').val()
		console.log(data)
		repairexit(data)
		
	})
	function repairexit(data){
		Load()
		$.ajax({
			type:'post',
			url:"{:url('adminapi/Insurance/edit')}",
			data:data,
			success:function(res){
				Load(1)
				console.log(res)
				if(res.code==1){
					Msg('保存成功')
					window.history.go(0)
				}else{
					Err(res.msg)
				}
			},
			error:function(){
				Err('请求失败')
			}
		})
	}
	
	
	{else/}
	$('.keep').on('click',function(){
		data.company_id=$('.company_all_id').val();
		$('input').each(function(){
			if($(this).attr('name')){
				data[$(this).attr('name')]=$(this).val()
			}
		})
		data.scale=$('select[name="scale"]').val()
		data.remark=$('textarea').val()
		console.log(data)
		
		repairadd(data)
		
	})
	
	function repairadd(data){
		Load()
		$.ajax({
			type:'post',
			url:"{:url('adminapi/Insurance/add')}",
			data:data,
			success:function(res){
				Load(1)
				console.log(res)
				if(res.code==1){
					Msg('添加成功')
					window.location.href='{:url("index")}'
				}else{
					Err(res.msg)
				}
			},
			error:function(){
				Err('请求失败')
			}
		})
	}
	{/if}
})

